package util.Img;

import util.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Servlet3UpLoadImg {
    private final static String path = "D:\\Program Files\\ideaProgram\\updateFiles\\";
    private final static long MAX_SIZE = 10*1024*1024;
    public static Result load(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        Part part = req.getPart("filename");
        String filename = part.getSubmittedFileName();
        filename = UUID.randomUUID().toString().replace("_","")+"-"+filename;
        if(part.getSize()<MAX_SIZE){
            part.write(path+filename);
            Map<String,Object> map = new HashMap<>();
            map.put("filename",filename);
            return Result.success(map);
        }else{
            return Result.error();
        }
    }
}
